package com.huike.web.controller.report;

import com.github.pagehelper.Page;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.entity.MPSysDictType;
import com.huike.common.entity.MPTbContract;
import com.huike.report.MpService.ReportContractStaticService;
import com.huike.report.domain.dto.ReportContractStatisticsListDTO;
import com.huike.report.domain.dto.ReportSalesDeptStatisticsListVo;
import com.huike.report.domain.vo.LineChartVo;
import com.huike.report.domain.vo.ReportContractStaticVo;
import com.huike.report.domain.vo.ReportSubjectVo;
import com.huike.web.result.ReportSubjectStatisticsData;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "客户统计相关接口")
@RequestMapping("/report")
public class ReporterController {

    @Autowired
    private ReportContractStaticService reportContractStaticService;

    /**
     * 客户统计明细列表
     * @param reportContractStatisticsListDTO
     * @return
     */
    @GetMapping("/contractStatisticsList")
    public TableDataInfo contractStatisticsList(ReportContractStatisticsListDTO reportContractStatisticsListDTO){
        log.info("{}",reportContractStatisticsListDTO);
        Page<MPTbContract> reportContractStaticVoPage = reportContractStaticService.pageList(reportContractStatisticsListDTO);

        List<ReportContractStaticVo> reportContractStaticVoList = new ArrayList<>();
        for (MPTbContract mpTbContract : reportContractStaticVoPage.getResult()) {
            ReportContractStaticVo reportContractStaticVo1 = new ReportContractStaticVo();
            BeanUtils.copyProperties(mpTbContract,reportContractStaticVo1);
            reportContractStaticVo1.setOrder(mpTbContract.getContractOrder());
            reportContractStaticVoList.add(reportContractStaticVo1);
        }
        TableDataInfo objectTableDataInfo = new TableDataInfo();
        objectTableDataInfo.setTotal(reportContractStaticVoPage.getTotal());
        objectTableDataInfo.setRows(reportContractStaticVoList);
        objectTableDataInfo.setCode(200);
        return objectTableDataInfo;

    }

    /**
     * 客户统计新增客户数报表
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @GetMapping("/contractStatistics/{beginCreateTime}/{endCreateTime}")
    public LineChartVo lineChart(@PathVariable String beginCreateTime,
                                 @PathVariable String endCreateTime){
        log.info("客户统计新增客户数报表：{}{}",beginCreateTime,endCreateTime);
        LineChartVo lineChartVo = reportContractStaticService.lineChart(beginCreateTime,endCreateTime);
        return lineChartVo;
    }

    @GetMapping("/subjectStatistics/{beginCreateTime}/{endCreateTime}")
    public AjaxResult reportSubject(@PathVariable String beginCreateTime,
                                    @PathVariable String endCreateTime){

        List<ReportSubjectVo> resultList =
                reportContractStaticService.reportSubject(beginCreateTime,endCreateTime);
        return AjaxResult.success(resultList);
    }


    /**
     * 销售统计归属部门明细列表
     * @return
     */
    @GetMapping("/deptStatisticsList/{beginCreateTime}/{endCreateTime}")
    public TableDataInfo deptStatisticsList(@PathVariable String beginCreateTime,
                                            @PathVariable String endCreateTime){
        Page<ReportSalesDeptStatisticsListVo> resultList = reportContractStaticService.deptStatisticsList(beginCreateTime,endCreateTime);
        TableDataInfo objectTableDataInfo = new TableDataInfo();
        objectTableDataInfo.setTotal(resultList.getTotal());
        objectTableDataInfo.setRows(resultList.getResult());
        objectTableDataInfo.setCode(200);
        return objectTableDataInfo;
    }
}
